<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;



use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\SendMsgModel;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use app\crm\model\ReserveandLost;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\common\traits\ActionRecordTrait;

use app\shop\model\unionpay\AppConfig;
use app\shop\model\unionpay\AppUtil;
use app\shop\model\unionpay\Test;
use app\crm\model\MiniProgramModel;


class Wxsend extends ApiCommon
{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['check_service_log','wx_send','daochu_wenjuan'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    

    public function wx_send()
    {

        $gzh_openids = Db::name('mini_program_fans')->alias('m')

        ->join('wx_gzh_fans g', 'g.unionId = m.unionId', 'LEFT')

        ->where([['g.openid','>','0']])

        ->column('g.openid,m.customerId');

        // echo "<pre>";
        // print_r($gzh_openids);
        // echo "</pre>";exit;
        //遍历查询--去重非直营店公众号触达

        
        $new_openid = [];
        $k = 0;
        foreach ($gzh_openids as $key => $value) {
           
            $gzh_openids_t = Db::name('customer')->alias('c')

            ->join('shop s', 'c.shop_id = s.shop_id', 'LEFT')
    
            ->where([['c.id','=',$value['customerId']]])

            ->where([['s.type','=', 1]])  //直营门店

            ->where([['s.status','=', 1]])  // 未关闭门店
    
            ->column('c.customer_name,s.shop_name');


            if(!empty($gzh_openids_t)){

                $new_openid[$k] = $value['openid'];

                $k++;
            }
             
        }

      
        $model = new SendMsgModel();

        $num = 0;
        foreach ($new_openid as $key => $value) {
            $model->wx_send_cs($value,0);
            $num++;
        }

        //echo "发送".count($gzh_openids).'-成功'.$num;

        return resultArray(['data'=>[],'error'=>'处理成功']);
       
    }

    

}